package test5.lupeng.jf.com.teamdemo;

import android.content.SharedPreferences;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

import java.util.ArrayList;

import test5.lupeng.jf.com.teamdemo.fragment.FoundFragment;
import test5.lupeng.jf.com.teamdemo.fragment.HomePageFragment;
import test5.lupeng.jf.com.teamdemo.fragment.MyselfFragment;
import test5.lupeng.jf.com.teamdemo.fragment.StudyFragment;
import test5.lupeng.jf.com.teamdemo.untils.UserInfo;
import test5.lupeng.jf.com.teamdemo.widget.BottomLayout;
import test5.lupeng.jf.com.teamdemo.widget.TabBean;

//Android团队项目：TeamDemo
public class MainActivity extends AppCompatActivity {

    private TextView tv_toolbar;
    private ArrayList<Fragment> mFragments;
    HomePageFragment homePageFragment  =new HomePageFragment();
    StudyFragment studyFragment  =new StudyFragment();
    FoundFragment foundFragment  =new FoundFragment();
    MyselfFragment myselfFragment  =new MyselfFragment();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

        saveUserInfo();
    }

    /**
     * 初始化视图
     */
    private void initView() {
        tv_toolbar = (TextView) findViewById(R.id.tv_toolbar);

        //四个模块的视图
        ArrayList<TabBean> tabBeen = new ArrayList<>();
        tabBeen.add(new TabBean("首页", R.drawable.ic_homepage));
        tabBeen.add(new TabBean("学习", R.drawable.ic_study));
        tabBeen.add(new TabBean("发现", R.drawable.ic_found));
        tabBeen.add(new TabBean("我的", R.drawable.ic_myself));

        BottomLayout bottomLayout = (BottomLayout) findViewById(R.id.bottomLayout);
        bottomLayout.setResource(tabBeen);

        //初始化四个模块
        mFragments = new ArrayList<>();


        mFragments.add(homePageFragment);
        mFragments.add(studyFragment);
        mFragments.add(foundFragment);
        mFragments.add(myselfFragment);
        selectFragment(0);
        setToolbar("首页");

        bottomLayout.setFragmentChangeListener(new BottomLayout.FragmentChangeListener() {
            @Override
            public void setFragment(int position) {
                selectFragment(position);
                switch (position){
                    case 0:
                        setToolbar("首页");
                        break;
                    case 1:
                        setToolbar("博客");
                        break;
                    case 2:
                        setToolbar("发现");
                        break;
                    case 3:
                        setToolbar("我的");
                        break;
                }
            }
        });
    }

    //从本地获取用户的ID
    public void  saveUserInfo(){
        SharedPreferences sp = getSharedPreferences("config", MODE_PRIVATE);
        UserInfo.userId = sp.getString("objectId","");
        UserInfo.userName = sp.getString("userName", "");
    }


    /**
     * 设置标题
     *
     * @param title 标题文字
     */
    private void setToolbar(String title) {
        if (tv_toolbar != null) {
            tv_toolbar.setText(title);
        }
    }

    private void  selectFragment(int position){
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction  = manager.beginTransaction();
        transaction.replace(R.id.frameLayout,mFragments.get(position));

        transaction.commit();
    }
}
